package com.wqiuqiu.gulimall.product.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.wqiuqiu.gulimall.product.dao.BrandDao;
import com.wqiuqiu.gulimall.product.dao.CategoryDao;
import com.wqiuqiu.gulimall.product.entity.BrandEntity;
import com.wqiuqiu.gulimall.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Key;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wqiuqiu.common.PageUtils;
import com.wqiuqiu.common.Query;

import com.wqiuqiu.gulimall.product.dao.CategoryBrandRelationDao;
import com.wqiuqiu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.wqiuqiu.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;
    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        String brandId = (String) params.get("brandId");
//        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
//        if (!brandId.isEmpty()){
//            wrapper.eq("brand_id", brandId);
//        }
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        categoryBrandRelation.setBrandName(brandDao.selectById(brandId).getName());
        categoryBrandRelation.setCatelogName(categoryDao.selectById(catelogId).getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrandName(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        this.update(categoryBrandRelationEntity,new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    @Override
    public List<BrandVo> queryCategoryBrands(Long catId) {
        List<CategoryBrandRelationEntity> entities = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandVo> brandVos = entities.stream().map(obj -> {
            BrandVo brandVo = new BrandVo();
            BeanUtils.copyProperties(obj, brandVo);
            return brandVo;
        }).collect(Collectors.toList());
        return brandVos;
    }

}